<?php


namespace app\admins\controller;


use app\BaseController;
use app\Request;
use app\validate\AdminCheck;
use think\exception\ValidateException;
use think\facade\Db;

class BookList extends BaseController
{
    protected $middleware = [\app\middleware\Check::class];
    public function index(){
        return view();
    }

    //    修改图书状态
    public function editState(){
        $data = request()->param();
        $admin = \app\model\Book::find($data['id']);
        $admin->status = $data['state'];
        $ret = $admin->save();
        if($ret){
            return  json(["state"=>200]);
        }
        else{
            return json(["state"=>204,"message"=>'修改状态失败']);
        }
    }

    //    修改图书
    public function edit(){
        $data = request()->param();
        $book = \app\model\Book::find($data['id']);
        $book->bname = $data['bname'];
        $book->author =  $data['author'];
        $book->publisher_id   = $data['publisher_id'];
        $book->category_id    = $data['category_id'];
        $book->count = $data['count'];
        $book->price = $data['price'];
        $book->update_at  = $data['update_at'];
        $ret = $book->save();
        $store = \app\model\Storage::find($data['id']);
        $store->address = $data['address'];
        $ret = $store->save();
        if($ret){
            return  json(["state"=>200]);
        }
        else{
            return json(["state"=>204,"message"=>'修改信息失败']);
        }
    }

    //    添加图书
    public function addBook(){
        $data = request()->param();
//        return $data;
            $data1 = [
                'bname' => $data['bname'],
                'author' =>  $data['author'],
                'publisher_id'    => $data['publisher_id'],
                'category_id'     => $data['category_id'],
                'count' => $data['count'],
                'price' => $data['price'],
                'create_at' => $data['create_at'],
                'update_at' => $data['create_at'],
            ];

            $Db = Db::name('book');
            $Db2 = Db::name('storage');


            $ret = $Db->insert($data1);
            $book = $Db->where('bname','=',$data['bname'])->find();
            $book_id = $book['id'];
//            return $book_id;
            $data2 =[
                'id' => $book_id,
                'address' => $data['address']
            ];
            $ret = $Db2->insert($data2);

            if($ret){
                return  json(["state"=>200]);
            }
            else{
                return json(["state"=>204,"message"=>'添加书籍失败']);
            }


    }

    //    删除图书
    public function delete()
    {
        if(request()->isGet()){
            $data = input('get.');
            Db::name('book')->where('id',$data['id'])->delete();
        }

    }

    //    分页展示
    public function article(Request $request){

//        查询条件
        $where = [];
        if (!empty($request->param('name'))) {
            $where[] = ['bname|id|author', 'like', "%{$request->param('name')}%"];
        }
        //获取每页显示的条数
        $limit= $request->param('limit');
        //获取当前页数
        $page= $request->param('page');
        $list = Db::name('book')
            ->where($where)
            ->alias('b')
            ->join('storage c','b.id = c.id')
            ->paginate($limit,false,['page'=>$page])
            ->toArray();
        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }
}